var Sofia = {
	form_id: '',
	campos: new Array(), //id do campo => conteudo
	sessao: new Array(), //id do campo => conteudo
	pagina_corrente: 1, // pagina atual da paginação
	itens_por_pagina: 20, //quantiade de registro apresentado na tabela

	Alert: function(titulo, mensagem){
		jAlert(mensagem, titulo);
	},

	ShowImagem: function(titulo, imagem){
		jAlert('<img src="' + imagem + '" border="0" />', titulo);
	},
	
	Prompt: function(msn, title, funcao){
		jPrompt(msn, [value, title, funcao]);
	},

	Salvar: function(object){
		var id_form = object.form.getAttribute('id');
		var action = this.NovoElemento('input', 'action', 'salvar', 'hidden', 'action');
		var form = document.getElementById(id_form);
		form.appendChild(action);

		object.form.submit();
	},

	Editar: function(id, page_name){
		var url = "?page=" + page_name + "&action=editar&id=" + id;
		location.href = url;		
	},

	Excluir: function(id, page_name){
		if(confirm("Você tem certeza que deseja remover o registro com id: " + id + "?")){
			$.ajax({ 
				url: "?page=" + page_name + "&action=excluir",
			    global: false,
			    type: "POST",
			    data: ({id : id}),
			    dataType: "html",
			    success: function(msg){
					Sofia.Alert("Resultado", msg);
			    }
			});
			this.MudaPagina(this.pagina_corrente, page_name);
		}
	},

	Submit: function(object){		
		object.form.submit();
	},

	NovoElemento: function(tipo, id, value, type, name, style){
		var newElement = document.createElement(tipo);

		newElement.setAttribute('id', id);
		newElement.setAttribute('value', value);
		newElement.setAttribute('name', name);
		newElement.setAttribute('style', style);
		newElement.setAttribute('type', type);

		return newElement;
	},

	MudaPagina: function(pagina, page_name){
		this.pagina_corrente = pagina;

		$.ajax({ 
			url: "?page=" + page_name + "&action=GeraDataGrid",
		    global: false,
		    type: "POST",
		    data: ({corrente : pagina}),
		    dataType: "html",
		    success: function(msg){
				$(".data-grid").html(msg);
		    }
		});
	},

	ConfigItensPorPagina: function(select, page_name){
		var valor = select.options[select.selectedIndex].value;

		$.ajax({ 
			url: "?page=" + page_name + "&action=GeraDataGrid",
		    global: false,
		    type: "POST",
		    data: ({corrente: this.pagina_corrente, itens_por_pagina: valor}),
		    dataType: "html",
		    success: function(msg){
				$(".data-grid").html(msg);
		    }
		});
	},

	urlDecode: function(string, overwrite){
        if(!string || !string.length){
                return {};
        }
        var obj = {};
        var pairs = string.split('&');
        var pair, name, value;
        var lsRegExp = /\+/g;
        for(var i = 0, len = pairs.length; i < len; i++){
                pair = pairs[i].split('=');
                name = unescape(pair[0]);
                value = unescape(pair[1]).replace(lsRegExp, " ");
                //value = decodeURIComponent(pair[1]).replace(lsRegExp, " ");
                if(overwrite !== true){
                        if(typeof obj[name] == "undefined"){
                                obj[name] = value;
                        }else if(typeof obj[name] == "string"){
                                obj[name] = [obj[name]];
                                obj[name].push(value);
                        }else{
                                obj[name].push(value);
                        }
                }else{
                        obj[name] = value;
                }
        }
        return obj;
	},

	getVar: function(param){
	        var wl = window.location.href;
	        var params = this.urlDecode(wl.substring(wl.indexOf("?")+1));
	        return(params[param]);
	},

	addEventHandler: function(object,event,handler,captures){
	    if(typeof(window.event) != "undefined"){
	        object.attachEvent("on"+event,handler);
	    }
	    else{
	        object.addEventListener(event,handler,captures);
	    }
	}
};